<?php
/**
 * 地理微博相关api
 * 注意：需要依赖defines.php,请在包含此文件之前包含defines.php
 */
/***************************变量引入***************************/

/***************************常量定义***************************/

/***************************类定义***************************/
/**
 * 一条记录
 */
class GeoWeiboRecord {
	public $id;
	public $msg;
    public $photo;
    public $latitude;
    public $longtitude;
	public $comment_count;
	public $userid;
	public $username;
	public $uptime;
	public $faceUrl;
    
    function __construct($id, $msg, $photo, $latitude, $longtitude, $comment_count, $userid, $username, $uptime, $faceUrl) {
		$this->id = $id;
        $this->msg = $msg;
		$this->photo = $photo;
        $this->latitude = $latitude;
        $this->longtitude = $longtitude;
		$this->comment_count = $comment_count;
		$this->userid = $userid;
		$this->username = $username;
		$this->uptime = $uptime;
		$this->faceUrl = $faceUrl;
    }
}

/**
 * 一条记录
 */
class CommentRecord {
	public $userid;
	public $username;
	public $comment;
    public $addtime;
	public $faceUrl;
    
    function __construct($userid, $username, $comment, $addtime, $faceUrl) {
		$this->userid = $userid;
		$this->username = $username;
		$this->comment = $comment;
		$this->addtime = $addtime;
		$this->faceUrl = $faceUrl;
    }
}

/**
 * 上传消息中附带的图片
 * @param img_name 上传的图片的名字标签，用于在$_FILES中找到图片的索引；
 * @param latitude 纬度
 * @param longtitude经度
 * @param msg 消息
 * @param user_id 用户id
 * @return array 其中包含成功信息；
 */
function add($obj, $img_name, $latitude, $longtitude, $msg, $user_id)
{
    //判断经纬度
    if(!isset($latitude) || !isset($longtitude)) {
        $arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        
        return $arr;
    }

	//上传图片
	$photo_id = 0;
	if(isset($_FILES[$img_name]) && $_FILES[$img_name]['error'] == 0) {		//图片成功上传
		$photo_id = $obj->create(TABLE_GEOWEIBO_PHOTO,array(
			COL_ADDTIME	=> date('Y-m-d H:i:s'),
		));
		
		//上传
		$arrUpload = tsUpload($_FILES[$img_name],$photo_id,PHOTO_PATH,array('jpg','gif','png',"jpeg"),"../uploadfile");
		if($arrUpload){

			$obj->update(TABLE_GEOWEIBO_PHOTO,array(
				COL_PHOTO_ID=>$photo_id,
			),array(
				COL_PHOTONAME=>$arrUpload['name'],
				COL_PHOTOTYPE=>$arrUpload['type'],
				COL_PATH=>$arrUpload['path'],
				COL_PHOTOURL=>$arrUpload['url'],
				COL_PHOTOSIZE=>$arrUpload['size'],
			));

		}
	}
		
	//添加记录
	$curr_time = date('Y-m-d H:i:s');
    $geoweibo_id = $obj->create(TABLE_GEOWEIBO, array(
		COL_USER_ID		=> $user_id,
		COL_LATITUDE	=> $latitude,
		COL_LONGTITUDE	=> $longtitude,
		COL_PHOTO_ID	=> $photo_id,
		COL_MSG			=> $msg,
		COL_ADDTIME		=> $curr_time,
		COL_UPTIME		=> $curr_time
	));
	
	if($geoweibo_id == FALSE) {
		$arr = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_DB_INSERT
        );
	} else {
		$arr = array(
			FLAG_RET => NO_ERR,
			FLAG_INSERT_ID => $geoweibo_id
		);
	}
	
	return $arr;
}

/*
 * 查找所有的图片
 * 返回:图片信息或者错误信息(json)
 */
function search($obj)
{
	$result = $obj->findAll(TABLE_GEOWEIBO, null, null, null, null, null);
	$arr = array();
	$i = 0;

	foreach ($result as $row)
	{
		$id = $row[COL_GEOWEIBO_ID];
		$user_id = $row[COL_USER_ID];
		$msg = $row[COL_MSG];
		$photo_id = $row[COL_PHOTO_ID];		//图片Id,到用户端再进行查找将其转换为实际路径
		$latitude = $row[COL_LATITUDE];
		$longtitude = $row[COL_LONGTITUDE];
		$comment_count = $row[COL_COUNT_COMMENT];
		$uptime = $row[COL_UPTIME];
		$user = aac('user')->getOneUser($user_id);
		$faceUrl = $user["face"];
		
		//查找评论人昵称
		$username_t = $obj->find(TABLE_TS_USER_INFO,
								array(COL_USER_ID=>$user_id),
								"(". COL_USER_NAME .")");
		if($username_t == FALSE) {
			$username = "null"; 
		} else {
			$username = $username_t[COL_USER_NAME];
		}
		
		//查找图片路径
		$photo_t = $obj->find(TABLE_GEOWEIBO_PHOTO,
							array(COL_PHOTO_ID=>$photo_id),
							"(" . COL_PHOTOURL . ")");
		if($photo_t == FALSE) {
			$photo = null;
		} else {
			$photo = $photo_t[COL_PHOTOURL];
		}
		
		$arr[$i] = new GeoWeiboRecord($id, $msg, $photo, $latitude, $longtitude, $comment_count, $user_id, $username, $uptime, $faceUrl);
		$i++;
	}

	$ret = array(
		FLAG_RET => NO_ERR,
		FLAG_RECORD_LIST => $arr
	);
	
	return $ret;
}

/**
 * 搜索某条geoweibo的评论
 * @param type $obj 操作对象
 * @param type $geoweibo_id 要查询哪个geoweibo的评论
 */
function comment_search($obj, $geoweibo_id)
{
	$sort_by = COL_ADDTIME . ' desc';
	$result = $obj->findAll(TABLE_GEOWEIBO_COMMENT, 
							array(COL_GEOWEIBO_ID=>$geoweibo_id), 
							$sort_by, null, null, null);
	$arr = array();
	$i = 0;

	foreach ($result as $row)
	{
		$user_id = $row[COL_USER_ID];
		$comment = $row[COL_COMMENT];
		$addtime = $row[COL_ADDTIME];
		$user = aac('user')->getOneUser($user_id);
		$faceUrl = $user["face"];
		
		//查找评论人昵称
		$username_t = $obj->find(TABLE_TS_USER_INFO,
								array(COL_USER_ID=>$user_id),
								"(". COL_USER_NAME .")");
		if($username_t == FALSE) {
			$username = null; 
		} else {
			$username = $username_t[COL_USER_NAME];
		}
		
		$arr[$i] = new CommentRecord($user_id, $username, $comment, $addtime, $faceUrl);
		$i++;
	}

	$ret = array(
		FLAG_RET => NO_ERR,
		FLAG_RECORD_LIST => $arr
	);
	
	return $ret;
}
?>
